package com.youlu.campus.admin.tencentcloud.controller;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.admin.auth.entity.SystemRole;
import com.youlu.campus.admin.auth.service.RoleService;
import com.youlu.campus.admin.tencentcloud.entity.DnsRecordQueryVo;
import com.youlu.campus.admin.tencentcloud.entity.DomainRecordRequest;
import com.youlu.campus.admin.tencentcloud.entity.TencentDnsRecord;
import com.youlu.campus.admin.tencentcloud.entity.TencentDomainItem;
import com.youlu.campus.admin.tencentcloud.service.TencentCloudService;
import com.youlu.campus.admin.tencentcloud.service.TencentDnsRecordService;
import com.youlu.campus.admin.tencentcloud.service.TencentDomainItemService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 腾讯云 操作
 *
 * @Description: TencentCloudController
 * @Date 2025/8/19
 * @module TencentCloudController
 * @author: su
 */
@Slf4j
@RequestMapping("/tencent/cloud")
@RestController
public class TencentCloudController {

    @Autowired
    private TencentCloudService tencentCloudService;

    @Autowired
    private TencentDnsRecordService tencentDnsRecordService;

    @Autowired
    private TencentDomainItemService tencentDomainItemService;

    @Autowired
    private RoleService roleService;

    /**
     * 创建域名记录
     *
     * @param request
     * @return
     */
    @PostMapping("/create/domain-record")
    public QResult createDomainRecord(@RequestBody @Validated DomainRecordRequest request) {
        log.info("createDomainRecord  request:{}", JSON.toJSONString(request));
        String userId = AuthUtils.getUserId();
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        List<SystemRole> adminRole = roleList.stream().filter(r -> "admin".equals(r.getIdentifier())).collect(Collectors.toList());
        //判断是否是 管理员  或者 是不是创建者
        if (CollectionUtils.isEmpty(adminRole)) {
            return QResult.fail("当前用户没有权限,请联系管理员");
        }
        QResult result = tencentCloudService.createDomainRecord(request);
        return result;
    }


    /**
     * 获取域名列表
     *
     * @return 域名列表
     */
    @PostMapping("/domain-list")
    public QResult findAll(@RequestBody DnsRecordQueryVo queryVo) {
        String userId = AuthUtils.getUserId();
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        List<SystemRole> adminRole = roleList.stream().filter(r -> "admin".equals(r.getIdentifier())).collect(Collectors.toList());
        //判断是否是 管理员
        if (CollectionUtils.isEmpty(adminRole)) {
            return QResult.fail("当前用户没有权限,请联系管理员");
        }
        Page<TencentDomainItem> domain = tencentDomainItemService.findByPage(queryVo);
        return QResult.success(domain);
    }

    /**
     * 获取DNS解析列表
     *
     * @param queryVo
     * @return
     */
    @PostMapping("/record-list")
    public QResult getDnsRecordList(@RequestBody DnsRecordQueryVo queryVo) {
        String userId = AuthUtils.getUserId();
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        List<SystemRole> adminRole = roleList.stream().filter(r -> "admin".equals(r.getIdentifier())).collect(Collectors.toList());
        //判断是否是 管理员
        if (CollectionUtils.isEmpty(adminRole)) {
            return QResult.fail("当前用户没有权限,请联系管理员");
        }
        Page<TencentDnsRecord> recordList = tencentDnsRecordService.findByPage(queryVo);
        return QResult.success(recordList);
    }

    /**
     * 初始化数据接口
     *
     * @return
     */
    @PostMapping("/init")
    public QResult getDnsRecordList() {
        String userId = AuthUtils.getUserId();
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        List<SystemRole> adminRole = roleList.stream().filter(r -> "admin".equals(r.getIdentifier())).collect(Collectors.toList());
        //判断是否是 管理员
        if (CollectionUtils.isEmpty(adminRole)) {
            return QResult.fail("当前用户没有权限,请联系管理员");
        }
        tencentCloudService.initTencentCloudData();
        return QResult.success(null);
    }


}
